var e = require("../../../../@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var t = e(require("../../../../@babel/runtime/helpers/classCallCheck")), r = e(require("../../../../@babel/runtime/helpers/createClass")), n = e(require("./InterceptorManager")), s = e(require("./../helpers/Utils")), u = function() {
    function e(r) {
        (0, t.default)(this, e), Object.assign(this, {
            defaults: r
        }), this.__init();
    }
    return (0, r.default)(e, [ {
        key: "__init",
        value: function() {
            this.__initInterceptor(), this.__initDefaults(), this.__initMethods();
        }
    }, {
        key: "__initInterceptor",
        value: function() {
            this.interceptors = new n.default(), this.interceptors.use({
                request: function(e) {
                    return e.requestTimestamp = new Date().getTime(), e;
                },
                requestError: function(e) {
                    return Promise.reject(e);
                },
                response: function(e) {
                    return e.responseTimestamp = new Date().getTime(), e;
                },
                responseError: function(e) {
                    return Promise.reject(e);
                }
            });
        }
    }, {
        key: "__initDefaults",
        value: function() {
            var e = {
                suffix: "Request",
                baseURL: "",
                header: {
                    Accept: "application/json, text/plain, */*",
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                transformRequest: [ function(e, t) {
                    return e;
                } ],
                transformResponse: [ function(e, t) {
                    if ("string" == typeof e) try {
                        e = JSON.parse(e);
                    } catch (e) {}
                    return e;
                } ],
                validateStatus: function(e) {
                    return e >= 200 && e < 300;
                }
            };
            this.defaults = Object.assign({}, e, this.defaults);
        }
    }, {
        key: "__initMethods",
        value: function() {
            var e = this, t = this.defaults.suffix, r = {
                method: [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT" ]
            };
            for (var n in r) r[n].forEach(function(r, n) {
                e[r.toLowerCase() + t] = function(t, n) {
                    return e.__defaultRequest(Object.assign({}, n, {
                        method: r,
                        url: t
                    }));
                };
            });
            this.request = function() {
                return e.__defaultRequest.apply(e, arguments);
            }, this.all = function(e) {
                return Promise.all(e);
            };
        }
    }, {
        key: "__defaultRequest",
        value: function(e) {
            var t = this;
            "string" == typeof e && (e = Object.assign({}, {
                url: arguments[1]
            }, arguments[2]));
            var r = Object.assign({
                method: "GET",
                dataType: "json"
            }, this.defaults, e), n = r.baseURL, u = (r.header, r.validateStatus), i = {
                url: r.url,
                data: r.data,
                header: r.header,
                method: r.method,
                dataType: r.dataType
            };
            this.$$prefix && !s.default.isAbsoluteURL(i.url) && (i.url = s.default.combineURLs(this.$$prefix, i.url)), 
            n && !s.default.isAbsoluteURL(i.url) && (i.url = s.default.combineURLs(n, i.url));
            var a = function(e, t) {
                for (var r = 0, n = t.length; r < n; ) {
                    var s = t[r++], u = t[r++];
                    e = e.then(s, u);
                }
                return e;
            }, o = function(e, t, r, n) {
                return n.forEach(function(n) {
                    e = n(e, t, r);
                }), e;
            }, f = function(e) {
                var t = Object.assign({}, e, {
                    data: o(e.data, e.header, e.statusCode, r.transformResponse)
                });
                return u(e.statusCode) ? t : Promise.reject(t);
            }, l = [], c = [], d = Promise.resolve(i);
            return this.interceptors.forEach(function(e) {
                (e.request || e.requestError) && l.push(e.request, e.requestError), (e.response || e.responseError) && c.unshift(e.response, e.responseError);
            }), d = a(d, l), d = a(d = d.then(function(e) {
                var n = Object.assign({}, e, {
                    data: o(i.data, i.header, void 0, r.transformRequest)
                });
                return t.__http(n).then(f, f);
            }), c);
        }
    }, {
        key: "__http",
        value: function(e) {
            return new Promise(function(t, r) {
                e.success = function(e) {
                    return t(e);
                }, e.fail = function(e) {
                    return r(e);
                }, wx.request(e);
            });
        }
    } ]), e;
}();

exports.default = u;